package com.spring.jiminshiro.controller;

import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.spring.jiminshiro.model.User;
import lombok.Data;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/3/26
 */
@Controller
public class TestController {

    @RequestMapping("login")
    public String login() {
        return "login";
    }

    @RequestMapping("index")
    public String index() {
        return "index";
    }

    @RequestMapping("/logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            subject.logout();
        }
        return "login";
    }

    @RequestMapping("admin")
    @ResponseBody
    public String admin() {
        return "admin success";
    }

    @RequestMapping("edit")
    @ResponseBody
    public String edit() {
        return "edit success";
    }

    @RequestMapping("test")
    @ResponseBody
    public String test() {
        return "test success";
    }

    @RequestMapping("unauthorized")
    public String unauthorized() {
        return "unauthorized";
    }

    @RequestMapping("/loginUser")
    public String loginUser(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            HttpSession session) {
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            User user = (User) subject.getPrincipal();
            session.setAttribute("user", user);
            return "index";
        } catch (Exception e) {
            return "login";
        }
    }

    @Autowired
    private MongoTemplate mongoTemplate;

    @GetMapping("mongo")
    @ResponseBody
    public String mongo() {
//        MongoClient mongoClient = new MongoClient("192.168.55.132", 27017);
//        DBCollection dbCollection = (DBCollection) mongoClient.getDatabase("demo").getCollection("person");
//        List<DBObject> personList = new ArrayList<>();
//        for (int i = 0; i < 1000; i++) {
//            personList.add((DBObject) new Person("李小璐", i));
//        }
//        dbCollection.insert(personList);
        for (int i = 0; i < 100000; i++) {
            mongoTemplate.insert(new Person("李小璐", i));
        }
        Object object = mongoTemplate.findAll(Person.class);
        return "mongo";
    }

    @Data
    public class Person {
        private String name;
        private Integer age;

        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }
    }

}
